package projeto.ea975;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Lucas
 */

/*Classe cliente que contém os métodos para busca e extração de dados a partir de uma página HTML e popular o banco de dados a partir dos dados obtidos
*/
public class Client {
    public void recebePagina(URL url, File file) throws IOException {
        BufferedReader in =
                new BufferedReader(new InputStreamReader(url.openStream()));
 
         BufferedWriter out = new BufferedWriter(new FileWriter(file));
 
        String inputLine;
 
        while ((inputLine = in.readLine()) != null) {
            // Grava pagina no arquivo
            out.write(inputLine);
            out.newLine();
        }
 
        in.close();
        out.flush();
        out.close();
    }
    
    public void geraSaidaHtml(int catalogo, int codCurso) {
        URL url = null;
        StringBuilder urlStr = new StringBuilder();
        File file = new File("C:\\Users\\Lucas\\Desktop\\ea975\\projeto1\\projeto-ea975\\src\\projeto\\ea975\\temp.html");

        try {
            /* diferenciacao de urls*/
            if((catalogo<2012)&&(catalogo>97)){
                urlStr.append("http://www.dac.unicamp.br/sistemas/catalogos/grad/catalogo");
                urlStr.append(Integer.toString(catalogo));
                urlStr.append("/cursos/cpl");
                urlStr.append(Integer.toString(codCurso));
                urlStr.append(".html");
                url = new URL(urlStr.toString());
            }else if(catalogo>=2012){
                urlStr.append("http://www.dac.unicamp.br/sistemas/catalogos/grad/catalogo");
                urlStr.append(Integer.toString(catalogo));
                urlStr.append("/curriculoPleno/cp");
                urlStr.append(Integer.toString(codCurso));
                urlStr.append(".html");
                url = new URL(urlStr.toString());
            }
            new Client().recebePagina(url, file);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //TODO
    public void populaCurso(){
        
    }
    //TODO
    public void populaCurriculo(){
        
    }
    //TODO
    public void populaDisciplina(){
        
    }
    //TODO
    public void armazenaBanco(){
        
    }
}
